#ifndef __CHADEMOCHARGECTRL_H__
#define __CHADEMOCHARGECTRL_H__
#include "stm32f7xx_hal.h"
#include "chargeCtrl.h"



#define CHADEMO_MSG_LENGTH	8

#define EV_FRAMEID1	0x100
#define EV_FRAMEID2	0x101
#define EV_FRAMEID3	0x102
#define CHARGER_FRAMEID108	0x108
#define CHARGER_FRAMEID109	0x109

#define CHADEMO_PERIOD	100	

typedef enum
{
	EVSE_MSGID108	= 0x00,
	EVSE_MSGID109,
}CHAdeMO_EvseMsgID;
typedef enum
{
	V0_9 = 0x00,
	V0_9_1,
	V1,
	V2_0
}CHAdeMO_ProtocolNum;

#pragma pack(1)
typedef union
{
	uint8_t faultFlags;
	struct
	{
		uint8_t battOverVol:1;
		uint8_t battUnderVol:1;
		uint8_t battCurDeviation:1;
		uint8_t battHighTemp:1;
		uint8_t battVolDeviation:1;
		uint8_t reserved:3;
	}faultFlagBit;
}EvFaultFlags_u;

typedef union
{
	uint8_t statusFlags;
	struct
	{
		uint8_t evChargingEnabled:1;	//0: disabled			1: enabled
		uint8_t evShiftPosition:1;		//0: 'Parking'position	1: other position
		uint8_t chargingSystemErr:1;	//0: normal				1: error
		uint8_t evStatus:1;				//0: EV contactor close or during welding detection	1: EV contactor open or termination of welding detection
		uint8_t stopReqBeforeCharging:1;//0: No request			1: Stop request
		uint8_t reserved:3;
	}statusFlagBit;
}EvStatusFlags_u;

typedef union
{
	uint8_t weldingDetFlags;
	struct
	{
		uint8_t weldingDetectioneEnable:1;
		uint8_t reserved:7;
	}weldingDetFlagBit;
}weldingDetFlags_u;




/*
*
*/
typedef union
{
	uint8_t statusErrFlags;
	struct
	{
		uint8_t chargerStatus:1;		//bit 0:(0: standby, 	1: charging) 
		uint8_t chargerMalfunction:1;	//bit 1:(0: normal, 	1: error) 
		uint8_t chargerConnectorLock:1;	//bit 2:(0: open, 		1: locked) (explained as Energizing state in CHAdeMO2.0.1)
		uint8_t battIncompatibility:1;	//bit 3:(0: compatible, 1: incompatible)
		uint8_t vehicleMalfunction:1;	//bit 4:(0: normal, 	1: error) (explained as Charging system error in CHAdeMO2.0.1)
		uint8_t chargingStopCtrl:1;		//bit 5:(0: operating,  1: during stop control or stop condition) 
		uint8_t reserved:2;
	}statusErrFlagBit;
}EvseStatusErrFlags_u;

typedef struct
{
	//100
	uint8_t minChargeCur;		//byte0:	Unit 1A		0~200A	Minimum current required for charging
	uint16_t battMinVol;		//byte2~3:	Unit 1V		0~600V
	uint16_t battMaxVol;		//byte4~5:	Unit 1V		0~600V
	uint8_t battSOC;			//byte6:	Unit 1%		Fixed at 0x64 (100%)
	//101
	uint8_t maxChargingTimeSec;		//byte1:	Unit 10s		0~2540s		Maximum charging time(byte 1)
	uint8_t maxChargingTimeMin;		//byte2:	Unit 1min		0~255min	Maximum charging time(byte 1)while using the "Byte 2",Set the "Byte 1"to "FF"
	uint8_t estimatedChargingTime;	//byte3:	Unit 1min		0~254min
	uint16_t totalBattCap;		  	//byte5~6:	Uint 0.1kWh	0.0~25.5kWh
	//102
	uint8_t protocolNum;		//byte0:	CHAdeMO protocol number of vehicle	@ref enum CHAdeMO_ProtocolNum
	uint16_t battTarVol;		//byte2~3:	Unit 1V		0~600V
	uint8_t reqCurrent;			//byte3:	Unit 1A		0~255A
	EvFaultFlags_u faultFlag;	//byte4:	
	EvStatusFlags_u statusFlag;	//byte5:	
	uint8_t chargeState;		//byte6:	Unit 1%
}CHAdeMOEvChargingParameter;

typedef struct 
{
	uint8_t d1Status;
	uint8_t canRcvStatus;
	uint8_t jStatus;
	uint8_t d2Status;
	
}vehicleChargeStatus_t;


typedef struct
{
	//108
	weldingDetFlags_u evWeldingDetEnabled;//byte0:	(0: disable): Charger does not support the EV relay welding detection (SN method)	(Other than zero: enable)
	uint16_t chargerMaxVol;		//byte1~2:	Unit 1V		0~600V
	uint8_t chargerMaxCur;		//byte3:	Unit 1A		0~255A
	uint16_t thresholdVol;		//byte4~5:	Unit 1V		0~600V
	//109
	uint8_t protocolNum;		//byte0:	CHAdeMO protocol number of charger	@ref enum CHAdeMO_ProtocolNum
	uint16_t presentVol;		//byte1~2:	Unit 1V		0~600V
	uint8_t presentCur;			//byte3:	Unit 1A		0~255A
	EvseStatusErrFlags_u statusErrFlag;	//byte5:	
	uint8_t remainChargingTimeSec;		//byte6:	Unit 10s	0~2540s		remaining charging time(byte 1)
	uint8_t remainChargingTimeMin;		//byte7:	Unit 1min	0~255min	remaining charging time(byte 1)while using the "Byte 2",Set the "Byte 1"to "FF"
}CHAdeMOEvseChargingParameter;
#pragma pack()




void ChargingInit(void);
extern uint32_t chargingCountdown[]; 
extern void CHAdeMOChargingProcessCtrl(void);
extern void CHAdeMOMsgProc(uint8_t gunId,CHAdeMOEvChargingParameter *p_rcv,CHAdeMOEvseChargingParameter *p_send);


void CHAdeMOChargingCtrlEntry(ChargeCtrlPara_t chargeCtrlMsg);


#endif //__CHADEMOCHARGECTRL_H__
